﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Data.OracleClient;
namespace GSLMS.DAL.Oracle
{
	/// <summary>
	/// 数据访问类:LOAN_CONTRACTTMP
	/// </summary>
	public partial class LOAN_CONTRACTTMP : BaseDAL<GSLMS.Entity.TB_LOAN_CONTRACTTMP>
	{
		private readonly GSLMS.Core.Loger log = new Core.Loger();
		public LOAN_CONTRACTTMP()
		{}
		#region  Method
		/// <summary>
		///  增加一条数据
		/// </summary>
		public override bool Add(GSLMS.Entity.TB_LOAN_CONTRACTTMP model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_TMPNAME", model.TMPNAME),
								new OracleParameter("P_SAVEURL", model.SAVEURL),
								new OracleParameter("P_TMPTYPE", model.TMPTYPE),
								new OracleParameter("P_STATUS", model.STATUS),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_SYSTEMID", model.SYSTEMID),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE),
								new OracleParameter("P_TMPFLAG", model.TMPFLAG)};
  
			for (int i = 0; i < 10; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_loan.proc_contracttmp_add", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public override bool Update(GSLMS.Entity.TB_LOAN_CONTRACTTMP model)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", model.ID),
								new OracleParameter("P_TMPNAME", model.TMPNAME),
								new OracleParameter("P_SAVEURL", model.SAVEURL),
								new OracleParameter("P_TMPTYPE", model.TMPTYPE),
								new OracleParameter("P_STATUS", model.STATUS),
								new OracleParameter("P_REMARK", model.REMARK),
								new OracleParameter("P_SYSTEMID", model.SYSTEMID),
								new OracleParameter("P_CREATEBY", model.CREATEBY),
								new OracleParameter("P_CREATEDATE", model.CREATEDATE),
								new OracleParameter("P_TMPFLAG", model.TMPFLAG)};
  
			for (int i = 0; i < 10; i++)
			{
				parms[i].Direction = ParameterDirection.Input;
			}

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_loan.proc_contracttmp_update", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///  删除一条数据
		/// </summary>
		public override bool Delete(long ID)
		{
			bool flag = false;
			OracleParameter[] parms =new OracleParameter[]{
								new OracleParameter("P_ID", ID)};
  
				parms[0].Direction = ParameterDirection.Input;

			try
			{
                OracleHelper.ExecuteNonQuery(OracleHelper.connectionString, CommandType.StoredProcedure, "pkg_loan.proc_contracttmp_delete", parms);
				flag = true;
			}
			catch (Exception ex)
			{
				log.ExceptionHand(ex);
			}
			return flag;
		}

		/// <summary>
		///DataRowToModel 
		/// </summary>
		private GSLMS.Entity.TB_LOAN_CONTRACTTMP DataRowToModel(DataRow dr)
		{
			if (dr != null && dr.ItemArray.Length > 0)
			{
				GSLMS.Entity.TB_LOAN_CONTRACTTMP model = new GSLMS.Entity.TB_LOAN_CONTRACTTMP();
				if(dr["ID"]!=null && dr["ID"].ToString()!="")
				{
					model.ID=OracleHelper.ToLong(dr["ID"].ToString());
				}
				if(dr["TMPNAME"]!=null && dr["TMPNAME"].ToString()!="")
				{
					model.TMPNAME=dr["TMPNAME"].ToString();
				}
				if(dr["SAVEURL"]!=null && dr["SAVEURL"].ToString()!="")
				{
					model.SAVEURL=dr["SAVEURL"].ToString();
				}
				if(dr["TMPTYPE"]!=null && dr["TMPTYPE"].ToString()!="")
				{
                    model.TMPTYPE = OracleHelper.ToInt(dr["TMPTYPE"].ToString());
				}
				if(dr["STATUS"]!=null && dr["STATUS"].ToString()!="")
				{
                    model.STATUS = OracleHelper.ToInt(dr["STATUS"].ToString());
				}
				if(dr["REMARK"]!=null && dr["REMARK"].ToString()!="")
				{
					model.REMARK=dr["REMARK"].ToString();
				}
				if(dr["SYSTEMID"]!=null && dr["SYSTEMID"].ToString()!="")
				{
                    model.SYSTEMID = OracleHelper.ToInt(dr["SYSTEMID"].ToString());
				}
				if(dr["CREATEBY"]!=null && dr["CREATEBY"].ToString()!="")
				{
					model.CREATEBY=dr["CREATEBY"].ToString();
				}
				if(dr["CREATEDATE"]!=null && dr["CREATEDATE"].ToString()!="")
				{
					model.CREATEDATE=DateTime.Parse(dr["CREATEDATE"].ToString());
				}
				if(dr["TMPFLAG"]!=null && dr["TMPFLAG"].ToString()!="")
				{
                    model.TMPFLAG = OracleHelper.ToInt(dr["TMPFLAG"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public override GSLMS.Entity.TB_LOAN_CONTRACTTMP GetByID(long ID)
		{
			GSLMS.Entity.TB_LOAN_CONTRACTTMP sModel = new GSLMS.Entity.TB_LOAN_CONTRACTTMP();
			string sql = "";
			sql = "select * from TB_LOAN_CONTRACTTMP where id=" + ID.ToString();
			DataSet ds = OracleHelper.ExecuteDataset(sql);
			if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
				sModel = DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
			return null;
			}
			return sModel;
		}

		/// <summary>
		/// 分页提取数据
		/// </summary>
		public override List<GSLMS.Entity.TB_LOAN_CONTRACTTMP> GetList(GSLMS.Entity.TB_LOAN_CONTRACTTMP searchParam)
		{
			var list=new List<GSLMS.Entity.TB_LOAN_CONTRACTTMP>();
			DataTable dt = null;
			string tblName = "TB_LOAN_CONTRACTTMP";
			string sqlWhere="";
			string strSort="";
			int pageSize=20;
			int pageIndex=1;
			string strFields="*";
			int totalCount = 0;
			int countFlag = 1;

            //是否计算总数
            countFlag = searchParam.Pagination.ReCount ? 1 : 0;
            pageSize = (searchParam.Pagination.PageSize == -1 || searchParam.Pagination.PageSize>0) ? searchParam.Pagination.PageSize : 15;
            pageIndex = searchParam.Pagination.CurrentIndex > 0 ? searchParam.Pagination.CurrentIndex : 1;
            strSort = searchParam.StrSort;

            if (searchParam.TMPNAME != null && searchParam.TMPNAME.Trim() != "")
            {
                sqlWhere += " and TMPNAME like '%" + OracleHelper.ReplaceUnsafeSql(searchParam.TMPNAME.Trim()) + "%'";
            }

            if (searchParam.STATUS > -1)
            {
                sqlWhere += " and STATUS=" + searchParam.STATUS.ToString();
            }

            if (searchParam.SYSTEMID > -1)
            {
                sqlWhere += " and SYSTEMID=" + searchParam.SYSTEMID.ToString();
            }

            if ( searchParam.TMPTYPE > -1)
            {
                sqlWhere += " and TMPTYPE=" + searchParam.TMPTYPE.ToString();
            }

            if ( searchParam.TMPFLAG > -1)
            {
                sqlWhere += " and TMPFLAG=" + searchParam.TMPFLAG.ToString();
            }


			dt = OracleHelper.GetPageData(tblName, strFields, strSort, pageSize, pageIndex,countFlag, sqlWhere, out totalCount);

            if (searchParam.Pagination.ReCount)
            {
                searchParam.RowCount = totalCount;
                searchParam.Pagination.RowCount = totalCount;
            }

            if (dt != null && dt.Rows.Count > 0)
			{

                

				for (int i = 0; i < dt.Rows.Count; i++)
				{
					GSLMS.Entity.TB_LOAN_CONTRACTTMP model = new GSLMS.Entity.TB_LOAN_CONTRACTTMP();
					model = DataRowToModel(dt.Rows[i]);
					list.Add(model);
				}
			}
			else
			{
				return null;
			}
			return list;
		}

		#endregion  Method
	}
}

